#include <iostream>
#include <cstdio>
#include <vector>
#include <pthread.h>
#include <unistd.h>
namespace Mutex_Model
{
    // 封装系统调用锁的接口（将锁的初始化 销毁隐藏)
    class Mutex
    {
    public:
        // 初始化
        Mutex()
        {
            int n = pthread_mutex_init(&_mutex, nullptr);
            (void)n;
        }

        // 申请锁
        void Lock()
        {
            int n = pthread_mutex_lock(&_mutex);
            (void)n;
        }

        // 释放锁
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_mutex);
            (void)n;
        }

        // 销毁锁
        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }

    private:
        pthread_mutex_t _mutex;
    };

    // 封装锁的申请和释放
    class Mutex_Gurda
    {
    public:
        Mutex_Gurda(Mutex *mutex)
            : _mutex(mutex)
        {
            _mutex->Lock();
        }

        ~Mutex_Gurda()
        {
            _mutex->Unlock();
        }

    private:
        Mutex *_mutex;
    };
}